package org.example.Parent;

import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader;
import com.sun.xml.internal.ws.util.StringUtils;
import javafx.util.Pair;
import org.example.functions.SpeedDashboard;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
/**
 * @description: data是记录数据的变量，path是图片路径，op是模式常量（0为半圆，1为全圆），n是圆表示的范围[0,n]
 * @author: Tang
 * @date: 2022/11/4 2:14
 * @param:
 * @return:
 **/
public abstract class realInstrument extends JPanel implements Instrument {
    //数据
    protected double data;
    //图片
    protected Image image;
    //指针的起始坐标
    protected Pair<Integer, Integer> coordinate;
    //模式
    protected int op;
    //范围[0,n]
    protected final int n;

    public realInstrument(double data, String path, Mode op, int n) {
        //初始数据
        this.data = data;
        this.n = n;
        //图片
        if(path != null && !path.isEmpty()) {
            try {
                //path = URLDecoder.decode(path,"utf-8");
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "读取资源文件出错！！！，出错路径：" + path);
                System.exit(0);
            }
        }
        this.op = op.op;
        new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateData();
                realInstrument.super.repaint();
            }
        }).start();
    }

    //绘制方法
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        if (op == 1) {
            coordinate = new Pair<>(getWidth() / 2, getHeight() / 2);
        } else if (op == 0) {
            coordinate = new Pair<>(getWidth() / 2, getHeight());
        }

        int xCenter = coordinate.getKey();
        int yCenter = coordinate.getValue();

        //抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        //加载图片
        if(image != null) {
            g2d.drawImage(this.image, 0, 0, getWidth(), getHeight(), this);
        }
        //圆点
        final int minR = 30;
        g2d.fillOval(xCenter - minR / 2, yCenter - minR / 2, minR, minR);
        //设置指针粗细
        BasicStroke stroke = new BasicStroke(8.0f);
        g2d.setStroke(stroke);

        int xSecond = 0, ySecond = 0;
        int sLength = Math.min(xCenter,yCenter);
        if (op == 1) {
            xSecond = (int) (xCenter + sLength * Math.sin(((data / n) * 2 * Math.PI)));
            ySecond = (int) (yCenter - sLength * Math.cos(((data / n) * 2 * Math.PI)));
        } else if (op == 0) {
            xSecond = (int) (xCenter - sLength * Math.cos((data / n) * Math.PI));
            ySecond = (int) (yCenter - sLength * Math.sin((data / n) * Math.PI));
        }

        g2d.setColor(Color.red);
        g2d.drawLine(xCenter, yCenter, xSecond, ySecond);
    }

    public  enum Mode {
        wholeCircle(1),
        semiCircle(0);
        final int op;

        Mode(int op) {
            this.op = op;
        }
    }
}



